package com.woniu.service.impl;

import com.woniu.entity.Statement1;
import com.woniu.mapper.StatementDao;
import com.woniu.pojo.Attendence;
import com.woniu.pojo.AttendenceExample;
import com.woniu.pojo.Statement;
import com.woniu.pojo.StatementExample;
import com.woniu.service.StatementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StatementServiceImpl implements StatementService {
    @Autowired
    StatementDao statementDao;
    @Override
    public void add(Statement statement) {
        statementDao.insert(statement);
    }

    @Override
    public void deleteById(Integer statementId) {
    statementDao.deleteByPrimaryKey(statementId);
    }

    @Override
    public Statement findOne(Integer statementId) {
        return statementDao.selectByPrimaryKey(statementId);
    }

    @Override
    public void updateById(Statement statement) {
    statementDao.updateByPrimaryKey(statement);
    }

    @Override
    public List<Statement> findAll() {
        return statementDao.selectByExample(null);
    }

    @Override
    public void deleteByExample(List<Integer> asList) {
        StatementExample statementExample=new StatementExample();
        statementExample.createCriteria().andStatementIdIn(asList);
        statementDao.deleteByExample(statementExample);
    }

    @Override
    public String updateStatusById(Integer statementId) {
        Statement statement = statementDao.selectByPrimaryKey(statementId);
        if (statement.getReason().equals("休学")){
            statement.setReason("已复学");
        }else {
            statement.setReason("休学");
        }
        statementDao.updateByPrimaryKey(statement);
        return statement.getReason();
    }

    @Override
    public Statement1 findOne1(Integer statementId) {
        return statementDao.selectByPrimaryKey1(statementId);
    }

    @Override
    public List<Statement1> findAll1() {
        return statementDao.selectByExample1(null);
    }

    @Override
    public List<Statement1> findDate(Map<String, Object> mapCondition) {
        StatementExample statementExample=new StatementExample();
        StatementExample.Criteria criteria = statementExample.createCriteria();
        Statement statement = (Statement) mapCondition.get("statement");
        if(mapCondition.get("start")!=null&&!mapCondition.get("start").equals("")){
            criteria.andStartTimeGreaterThanOrEqualTo((Date) mapCondition.get("start"));
        }
        if(mapCondition.get("end")!=null&&!mapCondition.get("end").equals("")){
            criteria.andStopTimeLessThanOrEqualTo((Date) mapCondition.get("end"));
        }
        if(statement.getStudentId()!=null){
            criteria.andStudentIdEqualTo(statement.getStudentId());
        }
        if(statement.getClassId()!=null){
            criteria.andClassIdEqualTo(statement.getClassId());
        }
        return statementDao.selectByExample1(statementExample);

    }
}
